<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/6/2
 * Time: 14:02
 */

namespace App\Http\Controllers\Finance;

use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\EmployeeModel;
use App\Http\Model\WageModel;
use App\Http\Model\YearModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class WageController extends Controller
{

    /**
     * 个人工资
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     * @throws \Exception
     */
    public function personalWage(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工人参数失败',
            'id.integer' => '工人参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $wageModel = new WageModel();
            $this->data = $wageModel->personalWage($param);
        } else {
            throw new ApiException($validator->errors()->first(), 720101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 个人工资全部的施工分账明细
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     * @throws \Exception
     */
    public function personalWageWorkmanAccounts(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工人参数失败',
            'id.integer' => '工人参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $wageModel = new WageModel();
            $this->data = $wageModel->personalWageWorkmanAccounts($param);
        } else {
            throw new ApiException($validator->errors()->first(), 720101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 个人工资
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     * @throws \Exception
     */
    public function personalWageHandymanAccounts(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工人参数失败',
            'id.integer' => '工人参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $wageModel = new WageModel();
            $this->data = $wageModel->personalWageHandymanAccounts($param);
        } else {
            throw new ApiException($validator->errors()->first(), 720101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     * @throws \Exception
     */
    public function personalWageAttendance(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工人参数失败',
            'id.integer' => '工人参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $wageModel = new WageModel();
            $this->data = $wageModel->personalWageAttendance($param);
        } else {
            throw new ApiException($validator->errors()->first(), 720101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
 * @param Request $request
 * @return \Illuminate\Http\Response
 * @throws ApiException
 * @throws \Exception
 */
    public function personalWageMaterialAccounts(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工人参数失败',
            'id.integer' => '工人参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $wageModel = new WageModel();
            $this->data = $wageModel->personalWageMaterialAccounts($param);
        } else {
            throw new ApiException($validator->errors()->first(), 720101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     * @throws \Exception
     */
    public function personalWageLoan(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工人参数失败',
            'id.integer' => '工人参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $wageModel = new WageModel();
            $this->data = $wageModel->personalWageLoan($param);
        } else {
            throw new ApiException($validator->errors()->first(), 720101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     * @throws \Exception
     */
    public function personalWageLivingRecharge(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工人参数失败',
            'id.integer' => '工人参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $wageModel = new WageModel();
            $this->data = $wageModel->personalWageLivingRecharge($param);
        } else {
            throw new ApiException($validator->errors()->first(), 720101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     * @throws \Exception
     */
    public function personalWageLivingRefund(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工人参数失败',
            'id.integer' => '工人参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $wageModel = new WageModel();
            $this->data = $wageModel->personalWageLivingRefund($param);
        } else {
            throw new ApiException($validator->errors()->first(), 720101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 工人工资汇总
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function employeeWages(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'professionId' => 'nullable|integer',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:0',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'professionId.integer' => '工种参数类型错误',
            'startTime.date_format' => '起始时间格式错误',
            'endTime.date_format' => '截止时间格式错误',
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only(['projectId','professionId','startTime','endTime','page','length','search']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $employeeModel = new EmployeeModel();

            //确定当前工程年度时间，搜索当前工程年度项目中的所有工人
            $yearModel = new YearModel();
            $yearInfo = $yearModel->findYear(date('Y-m-d'));
            $param['startTime'] = $param['startTime'] == '' ? $yearInfo['startTime'] : $param['startTime'];
            $param['endTime'] = $param['endTime'] == '' ? $yearInfo['endTime'] : $param['endTime'];
            $param['isManager'] = 0;
            $lists = $employeeModel->lists($param);
            $countLists = $employeeModel->countLists($param);

            $wageModel = new WageModel();
            foreach ($lists as $key => $l) {
                $wages = $wageModel->personalWage(['id'=>$l->id,'startTime'=>$param['startTime'],'endTime'=>$param['endTime']]);
                $lists[$key]->wage = $wages;
            }
            $this->data = $this->tableFormat($lists,$countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 710101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 工人工资统计
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     * @throws \Exception
     */
    public function employeeWageCollect(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'professionId' => 'nullable|integer',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'professionId.integer' => '工种参数类型错误',
            'startTime.date_format' => '起始时间格式错误',
            'endTime.date_format' => '截止时间格式错误',
        ];
        $param = $request->only(['projectId','professionId','startTime','endTime','search']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $employeeModel = new EmployeeModel();
            $startTime = $param['startTime'];
            $endTime = $param['endTime'];

            //确定当前工程年度时间，搜索当前工程年度项目中的所有工人
            $yearModel = new YearModel();
            $yearInfo = $yearModel->findYear(date('Y-m-d'));
            $param['startTime'] = $yearInfo['startTime'];
            $param['endTime'] = $yearInfo['endTime'];
            $param['isManager'] = 0;
            //做全项目的工资汇总
            $allLists = $employeeModel->lists($param,true);

            //按照时间区间汇总工资
            $startTime = $startTime===''?$yearInfo['startTime']:$startTime;
            $endTime = $endTime===''?$yearInfo['endTime']:$endTime;

            //新增汇总
            $workmanAccountsSum = 0;
            $handymanAccountsSum = 0;
            $attendanceAccountsSum = 0;
            $materialOrderSum = 0;
            $loan = 0;
            $livingRecharge = 0;
            $livingRefund = 0;
            $wageModel = new WageModel();
            foreach ($allLists as $key => $l){
                $wages = $wageModel->personalWage(['id'=>$l->id,'startTime'=>$startTime,'endTime'=>$endTime]);
                $attendanceSum = 0;
                foreach ($wages as $k => $ll){
                    $workmanAccountsSum += $ll['workmanAccounts'];
                    $handymanAccountsSum += $ll['handymanAccounts'];
                    $attendanceSum += $ll['attendance'];
                    $materialOrderSum += $ll['materialAccounts'];
                    $loan += $ll['loan'];
                    $livingRecharge += $ll['livingRecharge'];
                    $livingRefund += $ll['livingRefund'];
                }
                $attendanceAccountsSum += $attendanceSum * $l->dayValue;
            }
            $income = $workmanAccountsSum+$handymanAccountsSum+$attendanceAccountsSum;
            $this->data = [
                'workmanAccounts'=>$workmanAccountsSum,
                'handymanAccounts'=>$handymanAccountsSum,
                'attendance'=>$attendanceAccountsSum,
                'materialAccounts'=>$materialOrderSum,
                'loan'=>$loan,
                'livingRecharge'=>$livingRecharge,
                'livingRefund'=>$livingRefund,
                'income'=>$income,
                'balance'=>$income-$materialOrderSum-$loan-$livingRecharge+$livingRefund
            ];
        } else {
            throw new ApiException($validator->errors()->first(), 711301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
    //工人工资清算

    //管理人员工资汇总
    /**
     * 工人工资汇总
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function managerWages(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'professionId' => 'nullable|integer',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:0',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'professionId.integer' => '工种参数类型错误',
            'startTime.date_format' => '起始时间格式错误',
            'endTime.date_format' => '截止时间格式错误',
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only(['projectId','professionId','startTime','endTime','page','length','search']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $employeeModel = new EmployeeModel();
            $startTime = $param['startTime'];
            $endTime = $param['endTime'];

            //确定当前工程年度时间，搜索当前工程年度项目中的所有工人
            $yearModel = new YearModel();
            $yearInfo = $yearModel->findYear(date('Y-m-d'));
            $param['startTime'] = $yearInfo['startTime'];
            $param['endTime'] = $yearInfo['endTime'];
            $param['isManager'] = 1;
            $lists = $employeeModel->lists($param);
            $countLists = $employeeModel->countLists($param);

            //按照时间区间汇总工资
            $startTime = $startTime===''?$yearInfo['startTime']:$startTime;
            $endTime = $endTime===''?$yearInfo['endTime']:$endTime;

            $wageModel = new WageModel();
            foreach ($lists as $key => $l) {
                $wages = $wageModel->personalWage(['id'=>$l->id,'startTime'=>$startTime,'endTime'=>$endTime]);
                $lists[$key]->wage = $wages;
            }
            $this->data = $this->tableFormat($lists,$countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 710101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 管理人员工资汇总统计
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     * @throws \Exception
     */
    public function managerWageCollect(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'professionId' => 'nullable|integer',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'professionId.integer' => '工种参数类型错误',
            'startTime.date_format' => '起始时间格式错误',
            'endTime.date_format' => '截止时间格式错误',
        ];
        $param = $request->only(['projectId','professionId','startTime','endTime','search']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $employeeModel = new EmployeeModel();
            $startTime = $param['startTime'];
            $endTime = $param['endTime'];

            //确定当前工程年度时间，搜索当前工程年度项目中的所有工人
            $yearModel = new YearModel();
            $yearInfo = $yearModel->findYear(date('Y-m-d'));
            $param['startTime'] = $yearInfo['startTime'];
            $param['endTime'] = $yearInfo['endTime'];
            $param['isManager'] = 1;
            //做全项目的工资汇总
            $allLists = $employeeModel->lists($param,true);

            //按照时间区间汇总工资
            $startTime = $startTime===''?$yearInfo['startTime']:$startTime;
            $endTime = $endTime===''?$yearInfo['endTime']:$endTime;

            //新增汇总
            $workmanAccountsSum = 0;
            $handymanAccountsSum = 0;
            $attendanceAccountsSum = 0;
            $materialOrderSum = 0;
            $loan = 0;
            $livingRecharge = 0;
            $livingRefund = 0;
            $wageModel = new WageModel();
            foreach ($allLists as $key => $l){
                $wages = $wageModel->personalWage(['id'=>$l->id,'startTime'=>$startTime,'endTime'=>$endTime]);
                $attendanceSum = 0;
                foreach ($wages as $k => $ll){
                    $workmanAccountsSum += $ll['workmanAccounts'];
                    $handymanAccountsSum += $ll['handymanAccounts'];
                    $attendanceSum += $ll['attendance'];
                    $materialOrderSum += $ll['materialAccounts'];
                    $loan += $ll['loan'];
                    $livingRecharge += $ll['livingRecharge'];
                    $livingRefund += $ll['livingRefund'];
                }
                $attendanceAccountsSum += $attendanceSum * $l->dayValue;
            }
            $income = $workmanAccountsSum+$handymanAccountsSum+$attendanceAccountsSum;
            $this->data = [
                'workmanAccounts'=>$workmanAccountsSum,
                'handymanAccounts'=>$handymanAccountsSum,
                'attendance'=>$attendanceAccountsSum,
                'materialAccounts'=>$materialOrderSum,
                'loan'=>$loan,
                'livingRecharge'=>$livingRecharge,
                'livingRefund'=>$livingRefund,
                'income'=>$income,
                'balance'=>$income-$materialOrderSum-$loan-$livingRecharge+$livingRefund
            ];
        } else {
            throw new ApiException($validator->errors()->first(), 711401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
    //管理人员工资清算
}